import axios from 'axios'
import store from './store'
import router from './router'

//http全局拦截
//token 要放在我们请求的header上面带回去给后端

export default function setAxios() {
    //请求拦截
    axios.interceptors.request.use(
        config => {
            if (store.state.token) {
                config.headers.token = store.state.token
            }
            return config;
        }
    )

    //每次请求有返回的，都先经过这里
    axios.interceptors.response.use(
        response => {
            if (response.status == 200) {
                const data = response.data;
                if (data.code == -1) {
                    //登录过期需要重新登录  清空vuex的token 和 localStorage 的 token 
                    store.commit('setToken', '');
                    localStorage.removeItem('token');
                    router.replace({ path: '/login' })
                }
                return data;
            }
            return response;
        }
    )
}